<extend name="Base/common" />
<block name="style">
	<link rel="stylesheet" type="text/css" href="__PUBLIC__/Api/css/order.css">
</block>
<block name="Content">

<div class="page-group">
    <div class="page page-current">
        <div class="content <if condition='$type eq 1'>register</if>">
            <form action="#" class="m-phone">
                <ul>
                    <li>
                    	<i class="icon-phone-num"></i>
                    	<input type="text" name="tel" id="tel" placeholder="请输入手机号">
                    </li>
                    <li>
                        <i class="icon-valid"></i><input type="text" name="validation" id="validation" placeholder="请输入验证码"><a class="get-valid" href="#" id="validate">获取验证码</a>
                    </li>
                    <li>
                    	<i class="icon-pwd1"></i>
                    	<input type="password" name="pwd" id="pwd" placeholder="请输入6-12位密码">
                    	<i class="show"></i>
                    </li>
                    <!-- <li>
                    	<i class="icon-pwd2"></i>
                    	<input type="password" name="repwd" id="repwd" placeholder="请再次输入密码">
                    	<i class="show"></i>
                    </li> -->
                </ul>
                <if condition="$type eq '1'">
                <label class="agree">
                	<input type="checkbox" name="agreement" id="agreement">
                	<span>已同意<a href="{:U('Public/agreement')}">帮工小哥用户协议</a></span>
                </label>
                </if>
                <div class="confirm">
                    <a class="button button-fill button-big" type="button" id="register" href="#" onclick="register()">确定</a>
                    <input type="hidden" name="type" id="type" value="{$type}"/>
                    <input type="hidden" name="referee" id="referee" value="{$referee}"/>
                </div>
            </form>
        </div>
    </div>
</div>

</block>

<block name="script">
<script type="text/javascript">

$("#validate").click(sendCode);
var clearCount;
var setTime;

function sendCode(){
	
	var tel = $.trim($("#tel").val()) || "";
	var type = $.trim($("#type").val()) || "1";
	var target = Think.U('Public/validate');
	
	if(!(/^1((3|4|5|8){1}\d{1}|70)\d{8}$/.test(tel))){ 
        $.alert("手机号码格式有误，请确认");  
        return false; 
    }
	
	$.post(target, {'tel':tel, 'type':type}, function(data){
		
		if(data.status == '200'){
			$.toast('发送成功');
			//倒计时120秒
			setTime = 120;
			clearCount = setInterval(countDown,1000);
		}else if(data.status == '108'){
			$.alert('账号已被注册');
		}else if(data.status == '109'){
			$.alert('账号不存在');
		}else if(data.status == '200'){
			$.alert('发送成功');
		}else{
			$.alert('发送失败，请刷新后重试');
		}
	});
}

function countDown(){
	$("#validate").unbind("click");
	$("#validate").html(setTime+"秒后重发");
	setTime--;
	if(setTime <= 0){
		clearInterval(clearCount);
		$("#validate").click(sendCode);
		$("#validate").html("获取验证码");
	}
}

$("input").on('input propertychange', validate);
$("#agreement").on('input click', validate);
var pass = 1;

function validate(){

	$("input").each(function(i){

			var telReg =/^1(3|4|5|7|8)\d{9}$/; 
			var valReg = /^\d{6}$/;
			var pwdReg = /^[0-9a-zA-Z_]{6,12}$/;
			var value = $.trim($(this).val()) || "";
			var tp = $(this).attr('type');
			var name = $(this).attr('name');
			var error = false;

			if(tp != 'hidden'){
			
				if(tp == 'checkbox' && !$(this).attr("checked")){
					pass = 4;
					return false;
				}else if(name == 'tel' && !telReg.test(value)){
					pass = 1;
					return false;
				}else if(name == 'validation' && !valReg.test(value)){
					pass = 2;
					return false;
				}else if(name == 'pwd' && !pwdReg.test(value)){
					pass = 3;
					return false;
				}else{
					pass = 0;
				}
			}
	});
}

function register(){
	
	if(pass == 1){
		$.toast('手机号码格式不正确');
	}else if(pass == 2){
		$.toast('验证码格式不正确');
	}else if(pass == 3){
		$.toast('密码格式不正确');
	}else if(pass == 4){
		$.toast('需同意《帮工小哥用户协议》');
		return false;
	}else if(pass == 0){
		
		var tel = $.trim($("#tel").val()) || "";
		var pwd = $.trim($("#pwd").val()) || "";
		var validation = $.trim($("#validation").val()) || "";
		var type = $.trim($("#type").val()) || "1";
		var referee = $.trim($("#referee").val());
		var target = Think.U('Public/register');
		
		$.post(target, {'tel':tel, 'pwd':pwd, 'validation':validation, 'type':type, 'referee':referee}, function(dataObj){
			
			if(dataObj.status == '422'){
				$.toast("参数异常");
			}else if(dataObj.status == '103'){
				$.toast('验证码错误');
			}else if(dataObj.status == '108'){
				$.toast('账号已被注册');
			}else if(dataObj.status == '109'){
				$.toast('账号不存在');
			}else if(dataObj.status == '301'){
				$.toast('操作失败');
			}else if(dataObj.status == '200'){
				var msg = type == 1 ? '注册成功' : '操作成功';
				$.alert(msg);
				window.location.href = Think.U('Public/login');
			}
		});
	}
}

</script>
</block>

